// pages/course/course.js
var api = require('../../utils/api.js');
var app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    idx1: '',
    idx2: '',
    value1:'全部分类',
    value2: '位置',
    rangeKey1:'a',
    rangeKey2: 'a',
    array1: [{ a: '瑜伽' }, { a: '瑜伽' }, { a: '瑜伽' }, { a: '瑜伽' },],
    array2: [{ a: '武汉' }, { a: '黄石' },],
    markers:[]
  },
  //获取当前城市信息
  loadCity(longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
      data: {
        location: `${latitude},${longitude}`,
        key: '4RPBZ-BQILD-DIR4K-HVJ4J-OZJES-GTF5Z'
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        // success  

        var cityInfor = res.data.result.address_component;
        var province = cityInfor.province;
        var city = cityInfor.city;
        var district = cityInfor.district
        var region = [province, city, district]
        console.log(region)
        this.setData({
          region
        })

      },
      fail: () => {
        this.setData({
          currentCity: "获取定位失败"
        });
      },

    })
  },

  bindRegionChange(e) {
    var region = e.detail.value
    this.setData({
      region
    })
   
  },

  changeSchool(e) {
    console.log(e)
    this.setData({
      idx1: e.detail.value
    })
   
  },

  changeClass(e) {
    this.setData({
      idx2: e.detail.value
    })
  
  },

  //跳转课程详情
  toDetail(e){
   
    var id = e.currentTarget.dataset.id;
    console.log(id)
    var schoolid = e.currentTarget.dataset.schoolid;
    wx.navigateTo({
      url: '../courseDetail/courseDetail?id=' + id + '&schoolid=' + schoolid,
    })
  },

  //获取课程列表
  getCourseList(){
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        this.setData({
          latitude,
          longitude
        })
        this.loadCity(longitude, latitude);

        var place = '' + this.data.latitude + ',' + this.data.longitude;
        var school_id = this.data.schoolId;
        api.fetchApi('course/course_list', { token: wx.getStorageSync('token'), school_id, place }).then(res => {
          if(res.length==0){
            wx.showToast({
              title: '暂无校区！',
              icon:'none'
            })
          }
          this.setData({
            courseList: res
          })
        })
      }
    })

   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var { schoolId} = options;
    if (options.item){
      var item = JSON.parse(options.item)
      var markers = [
        {
          iconPath: "http://qnwmfx.laigl.com/iconimg/marke.png",
          id: item.campus_is,
          title: item.campus_name + ' ' + item.address,
          latitude: item.lat,
          longitude: item.lng,
          width: 35,
          height: 35
        }
      ]
      this.setData({
        lat: item.lat,
        lng: item.lng,
        markers
      })
    }
    
    
    this.setData({
      schoolId
    })
  

 

    wx.getSetting({
      
      success(res) {
        console.log(111)
        if (!res.authSetting['scope.userLocation']) {
          console.log(222)
          // wx.authorize({
          //   scope: 'scope.userLocation',
          //   success() {
             
          //   }
          // })
        }else(
          console.log(555)
        )
      }
    })

   
    this.getCourseList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})